/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sahd.security;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

/**
 *
 * @author Levix
 */
public  class DataCipher {

    public Cipher getCipher() {
        return cipher;
    }

    Cipher cipher;
    SecretKey secretKey;
    private static final String encryptAlgorithmName="DES";
    private static final String cipherAlgorithmName="DES/CBC/PKCS5Padding";
 

    public void cipherinEncryptMode() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException{
      cipher = Cipher.getInstance(cipherAlgorithmName);
      cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        
    }
    public void cipherinDecryptMode() throws InvalidKeyException{
         IvParameterSpec dps = new IvParameterSpec(cipher.getIV());
        try {
            cipher.init(Cipher.DECRYPT_MODE, secretKey, dps);
        } catch (InvalidKeyException ex) {
            throw new InvalidKeyException();
        } catch (InvalidAlgorithmParameterException ex) {
            throw new InvalidKeyException();
        }
       
     
    }


     public void createSecretKey(String cipherKey) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException{

             SecretKeyFactory skf = SecretKeyFactory.getInstance(encryptAlgorithmName);
             DESKeySpec kspec = new DESKeySpec(cipherKey.getBytes());
             secretKey = skf.generateSecret(kspec);

    }

}




